package com.example.newnoke.view.dialog;

import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.newnoke.R;
import com.geya.jbase.uiview.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by jiang on 2017/5/10.
 */

public class PayDialgo extends BaseBottomDialog {


    PayCallBack payCallBack;

    private TextView mPrice, pay;
    private EditText mNum;
    private ImageView jia, jian;
    private LinearLayout mLayout;

    private String name;
    private int num = 1;
    private double price ;
    private String id;
    private String type;


    @Override
    public int getLayoutRes() {
        return R.layout.view_pay;
    }

    @Override
    public void bindView(View v) {
        mPrice = v.findViewById(R.id.price);
        pay = v.findViewById(R.id.pay);
        mNum = v.findViewById(R.id.num);
        jia = v.findViewById(R.id.jia);
        jian = v.findViewById(R.id.jian);
        mLayout = v.findViewById(R.id.ll);
        price = Double.parseDouble(getArguments().getString("price")) ;
        type = getArguments().getString("type");
        id = getArguments().getString("id");
        name = getArguments().getString("name");
        if (type.equals("1")){
            mLayout.setVisibility(View.INVISIBLE);
        }
        mPrice.setText(price+"");
        num = Integer.parseInt(mNum.getText().toString());
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNum.getText().toString().equals("")) {
                    mNum.setText("0");
                }
                num = Integer.parseInt(mNum.getText().toString());
                num++;
                if (num <= 0) {
                    num = 1;
                }
                mNum.setText(num + "".trim());
                mPrice.setText(price * num + "");
            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNum.getText().toString().equals("")) {
                    mNum.setText("0");
                }
                num = Integer.parseInt(mNum.getText().toString());
                num--;
                if (num <= 0) {
                    num = 1;
                }
                mNum.setText(num + "".trim());
                mPrice.setText(price+"");
                mPrice.setText(price * num + "");
            }
        });


        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNum.getText().toString().isEmpty()) {
                    ToastUtil.showShort("请选择要购买的数量");
                    return;
                }
                payCallBack.pay("1", name, num+"", price+"", "1",id);
                dismiss();
            }
        });

        mNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = mNum.getText().toString();
                Pattern p = Pattern.compile("[0-9]*");
                Matcher m = p.matcher(money);
                if (!mNum.getText().toString().isEmpty()) {
                    num = Integer.parseInt(mNum.getText().toString());
                    mPrice.setText(price * num + "");

                } else {
//                    ToastUtil.toastCenter(BuyCuurseActivity.this, "请输入正确的缴费金额");
//                    mNum.setText("1");
                    mPrice.setText("0");
                }

            }
        });
    }

    void toPay() {
        dismiss();
    }

    @Override
    public float getDimAmount() {
        return 0.4f;
    }

    public void show(FragmentManager fragmentManager, PayCallBack payCallBack) {
        show(fragmentManager, getFragmentTag());
        this.payCallBack = payCallBack;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public interface PayCallBack {
        /**
         *
         * @param type 类型
         * @param name 名称
         * @param num  数量
         * @param price 价格
         * @param payway 平台
         */
        void pay(String type, String name, String num, String price, String payway,String id);

    }


}
